package com.example.vv.model.accountDetails;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toolbar;

import com.example.vv.R;
import com.example.vv.fragmentWork.entity.BannerEntity;
import com.example.vv.fragmentWork.entity.NaviEntity;
import com.example.vv.fragmentWork.entity.ProjectListEntity;
import com.example.vv.fragmentWork.entity.WxarticleEntity;
import com.example.vv.fragmentWork.mvp.MvpActivity;
import com.example.vv.model.accountDetails.base.contract.AIDContract;
import com.example.vv.model.accountDetails.base.presenter.AIDPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AIDActivity extends MvpActivity<AIDContract.View, AIDPresenter> implements AIDContract.View {

    @BindView(R.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.aid_web)
    WebView aidWeb;

    @Override
    public int initLayout() {
        return R.layout.activity_a_i_d;
    }

    @Override
    public AIDPresenter initPresenter() {
        return new AIDPresenter();
    }

    @SuppressLint("NewApi")
    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        titleToolbar.setNavigationOnClickListener(v -> {
            finish();
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleView() {
        return null;
    }


    @Override
    public Context getMContext() {
        return getMContext();
    }

    /**
     * 接收公众号数据
     *
     * @param wxarticleEntity
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getData(WxarticleEntity.DatasBean wxarticleEntity) {

        titleContent.setText(wxarticleEntity.getAuthor());
        aidWeb.loadUrl(wxarticleEntity.getLink());
        initWeb();
    }
    /**
     * 接收广告栏数据
     *
     * @param bean
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getData(BannerEntity bean) {
        titleContent.setText(bean.getTitle());
        aidWeb.loadUrl(bean.getUrl());
        initWeb();
    }

    public void initWeb() {
        aidWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        aidWeb.setLayerType(View.LAYER_TYPE_HARDWARE,null);
        WebSettings settings = aidWeb.getSettings();
        settings.setSupportMultipleWindows(true);
        settings.setUseWideViewPort(true);
        settings.setAllowFileAccess(true);
        settings.setSupportZoom(false);
        settings.setDisplayZoomControls(false);
        settings.setBuiltInZoomControls(false);
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}